#!/usr/bin/env python
# -*- coding: UTF-8 -*-

#  browser.py
#  Gabriel Acosta - 2013

# This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Belén - Catamarca - Argentina - Diciembre 2013


import sys
import re

from PyQt4 import QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import QWebView
from libs import foo
from libs import show


class Navegador(QWidget):

    def __init__(self):
        super(Navegador, self).__init__()
        vbox = QVBoxLayout(self)

        #self.setWindowIcon(QtGui.QIcon('images/icono.png'))
        showIcon = show.show_Icon(self)

        set_tool_Tip = foo.tool_Tip(self)

        self.botonAtras = QPushButton(QtGui.QIcon('images/left.png'), '')
        self.botonSiguiente = QPushButton(QtGui.QIcon('images/right.png'), '')
        self.botonActualizar = QPushButton(QtGui.QIcon('images/refresh.png'), '')
        self.botonParar = QPushButton(QtGui.QIcon('images/stop.png'), '')
        self.barraUrl = QLineEdit()
        self.botonOk = QPushButton(QtGui.QIcon('images/go.png'), '')

        hbox = QHBoxLayout()
        hbox.addWidget(self.botonAtras)
        hbox.addWidget(self.botonSiguiente)
        hbox.addWidget(self.botonActualizar)
        hbox.addWidget(self.botonParar)
        hbox.addWidget(self.barraUrl)
        hbox.addWidget(self.botonOk)

        vbox.addLayout(hbox)

        self.web = QWebView(self)

        set_the_Page = show.show_Page(self)

        vbox.addWidget(self.web)

        self.estado = QStatusBar()
        self.estado.addWidget(QLabel('Cargando pagina...'))
        self.progreso = QProgressBar()
        self.estado.addWidget(self.progreso)
        vbox.addWidget(self.estado)

        self.connect(self.botonAtras, SIGNAL("clicked()"), self.web.back)
        self.connect(self.botonSiguiente, SIGNAL("clicked()"), self.web.forward)
        self.connect(self.botonActualizar, SIGNAL("clicked()"), self.web.reload)
        self.connect(self.botonParar, SIGNAL("clicked()"), self.web.stop)
        self.connect(self.barraUrl, SIGNAL("returnPressed()"), self.do_search)
        self.connect(self.web, SIGNAL("loadFinished(bool)"), self.load_terminado)
        self.connect(self.web, SIGNAL("loadStarted()"), self.estado.show)
        self.connect(self.web, SIGNAL("loadProgress(int)"), self.progreso.setValue)

    def load_terminado(self):
     self.estado.hide()
     self.barraUrl.setText(self.web.url().toString())

    def do_search(self):
        url = unicode(self.barraUrl.text())
        pat = re.compile('(.+)\\.(.+)')
        if pat.match(url) and not url.startswith('http://'):
            url = 'http://%s' % url
        elif not pat.match(url):
            url = 'http://google.com/search?q=' + url.replace(' ', '+' )
        self.web.load(QUrl(url))

# Creo el objeto aplicación
app = QApplication(sys.argv)

w = Navegador()
w.setWindowTitle('Navegador Web')
w.show()

# Loop principal
sys.exit(app.exec_())
